package com.zjj.crypt.advice;


import com.zjj.crypt.anno.ZjjDecryptRequest;
import com.zjj.crypt.anno.ZjjEncryptResponse;
import org.springframework.core.MethodParameter;

import java.util.Objects;

/**
 * 判断是否需要进行加密
 *
 * @author zjj_admin
 * @date 2023/2/23 10:00
 */
public class NeedCrypt {

    private NeedCrypt() {
    }


    /**
     * 判断是否需要对结果进行加密
     *
     * @param methodParameter
     * @return
     */
    public static boolean isNeedEncrypt(MethodParameter methodParameter) {
        //在类上面是否有 ZjjEncryptResponse 注解
        boolean classPresent = methodParameter.getContainingClass().isAnnotationPresent(ZjjEncryptResponse.class);
        //在方法上是否有 ZjjEncryptResponse 注解
        boolean methodPresent = Objects.requireNonNull(methodParameter.getMethod()).isAnnotationPresent(ZjjEncryptResponse.class);
        if (classPresent) {
            return true;
        }
        //当类上面没有加密注解时，就需要判断方法上有没有加密注解
        return methodPresent;
    }


    /**
     * 判断是否需要对参数进行解密
     *
     * @param methodParameter
     * @return
     */
    public static boolean isNeedDecrypt(MethodParameter methodParameter) {
        boolean classPresent = methodParameter.getContainingClass().isAnnotationPresent(ZjjDecryptRequest.class);
        boolean methodPresent = Objects.requireNonNull(methodParameter.getMethod()).isAnnotationPresent(ZjjDecryptRequest.class);
        // 当类上面有 DecryptRequest 注解时，表示所有的参数都是加密后的，需要对参数进行解密
        if (classPresent) {
            return true;
        }
        return methodPresent;
    }
}
